package com.zuikaku.servlet;

import com.zuikaku.dao.UserDao;
import com.zuikaku.pojo.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String inputUsername = req.getParameter("username");
        String inputPassword = req.getParameter("password");
        User user = UserDao.getUserByUserNameAndPassword(inputUsername,inputPassword);
        if(user==null){
            //todo 跳转错误页面
            String msg = "账户不存在或者密码错误";
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/page/error.jsp");
            req.setAttribute("msg",msg);
            requestDispatcher.forward(req,resp);
        }else{
            HttpSession session =req.getSession();
            session.setAttribute("activeUser",user);
            ServletContext servletContext = session.getServletContext();
            servletContext.setAttribute("notice","周5不上课");
            resp.sendRedirect(req.getContextPath()+"/home");

        }

    }
}
